package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ModuleErrorDao;
import com.sugon.entity.ModuleErrorEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.ModuleErrorService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 模具异常记录Service实现类
 *
 * @author sugon
 * @date 2019-08-02 11:49:37
 */
@Service("moduleErrorService")
public class ModuleErrorServiceImpl implements ModuleErrorService {
    @Autowired
    private ModuleErrorDao moduleErrorDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    /**
     * ]
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public ModuleErrorEntity queryObject(String id) {
        return moduleErrorDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
//    @DataFilter(userAlias = "CREATE_USER", deptAlias = "WORKSHOP")
    public List<ModuleErrorEntity> queryList(Map<String, Object> map) {
        String usersubShop = ShiroUtils.getDeptId();
        if (StrUtil.isNotEmpty(usersubShop) && usersubShop.length() >= 4) {
            usersubShop = usersubShop.substring(0, 4);
            map.put("workShop", usersubShop);
        }

        return moduleErrorDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
//    @DataFilter(userAlias = "CREATE_USER", deptAlias = "WORKSHOP")
    public int queryTotal(Map<String, Object> map) {

        String usersubShop = ShiroUtils.getDeptId();
        if (StrUtil.isNotEmpty(usersubShop) && usersubShop.length() >= 4) {
            usersubShop = usersubShop.substring(0, 4);
            map.put("workShop", usersubShop);
        }
        return moduleErrorDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param moduleError 实体
     * @return
     */
    @Override
    public int save(ModuleErrorEntity moduleError) {
        moduleError.setId(RandomUtil.randomString(32));
        moduleError.setCreateUser(getUserId());
        moduleError.setCreateTime(new Date());
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(moduleError.getId(), ProcessInfoEnum.MDE_YCJL, 0, null);
        int rows = moduleErrorDao.save(moduleError);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleError.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../module/moduleerror.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_YCJL, msgEntity, 0);
        }
        return rows;
    }

    /**
     * saveApi
     *
     * @param moduleError 实体
     * @param userId
     * @param deptId
     * @param id
     * @return
     */
    @Override
    public int saveApi(ModuleErrorEntity moduleError, String userId, String deptId, String id) {
        moduleError.setId(RandomUtil.randomString(32));
        moduleError.setCreateUser(userId);
        moduleError.setCreateTime(new Date());
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(id, ProcessInfoEnum.MDE_YCJL, 0, null);

        int rows = moduleErrorDao.save(moduleError);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleError.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../module/moduleerror.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_YCJL, msgEntity, 0);
        }
        return rows;
    }

    /**
     * update
     *
     * @param moduleError 实体
     * @return
     */
    @Override
    public int update(ModuleErrorEntity moduleError) {
        moduleError.setUpdateUser(getUserId());
        moduleError.setUpdateTime(new Date());
        int rows = moduleErrorDao.update(moduleError);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleError.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../module/moduleerror.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_YCJL, msgEntity, 0);
        }
        return rows;
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return moduleErrorDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return moduleErrorDao.deleteBatch(ids);
    }

    /**
     * review
     *
     * @param moduleError 实体
     * @return
     */
    @Override
    public int review(ModuleErrorEntity moduleError) {
        moduleError.setErrorDisStatus("1");
        moduleError.setErrorReviewUser(getUserId());
        moduleError.setErrorCheckTime(new Date());
        //添加审核 消息提醒
        sysProcessInfoService.saveProcessInfo(moduleError.getId(), ProcessInfoEnum.MDE_YCJL, Integer.valueOf(moduleError.getErrorDisStatus()), null);
        int rows = moduleErrorDao.update(moduleError);
        if (rows > 0) {
            SysMsgEntity sysMsgEntity = new SysMsgEntity();
            String Id = moduleError.getId();
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.MDE_YCJL);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
            sysMsgService.allMsgData(sysMsgEntity);
        }
        return rows;
    }

    /**
     * getInfoByMaintainRecord
     *
     * @param maintainRecord
     * @return
     */
    @Override
    public ModuleErrorEntity getInfoByMaintainRecord(String maintainRecord) {
        return moduleErrorDao.getInfoByMaintainRecord(maintainRecord);
    }
}
